/*
 * Copyright (c) 2023 huipei.x
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.xphsc.kubernetes.core.support;

import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.util.Config;
import io.kubernetes.client.util.KubeConfig;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;

/**
 * {@link }
 * @author <a href="xiongpeih@163.com">huipei.x</a>
 * @description:
 * @since 1.0.0
 */
public class KubernetesConfigBuilder {

    private String kubeConfigPath;
    private String url;
    private String token;
    private boolean validateSSL;
    private String username;
    private String password;

    private String kubeConfig;
    private int connectionTimeout;
    private int readTimeout;
    private int writeTimeout;
    public KubernetesConfigBuilder() {
    }

    public ApiClient initClient() {
        ApiClient client = null;
        InputStream inputStream = null;
        try {
            if(StringUtils.isNotBlank(kubeConfigPath)&&StringUtils.isBlank(kubeConfig)){
                inputStream = ResourceRenderer.resourceLoader(kubeConfigPath);
                client = Config.fromConfig(inputStream);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if(StringUtils.isNotBlank(kubeConfig)){
                client= Config.fromConfig(KubeConfig.loadKubeConfig(new StringReader((String) kubeConfig)));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(StringUtils.isNotBlank(url)){
            if(StringUtils.isNotBlank(token)){
                if(!validateSSL){
                    client = Config.fromToken(url,token,false);
                }
            }else if (StringUtils.isNotBlank(username)&&StringUtils.isNotBlank(password)){
                if(!validateSSL){
                    client = Config.fromUserPassword(url,username,password,false);
                }
            }else{
                if(!validateSSL){
                    client = Config.fromUrl(url,false);
                }
            }
        }
        client.setConnectTimeout(connectionTimeout);
        client.setReadTimeout(readTimeout);
        client.setWriteTimeout(writeTimeout);
        Configuration.setDefaultApiClient(client);
        return client;
    }

    public static KubernetesConfigBuilder.Builder builder() {
        return new KubernetesConfigBuilder.Builder();
    }

    private KubernetesConfigBuilder(KubernetesConfigBuilder.Builder builder) {
        this.kubeConfigPath = builder.kubeConfigPath;
        this.url = builder.url;
        this.token = builder.token;
        this.validateSSL = builder.validateSSL;
        this.username = builder.username;
        this.password = builder.password;
        this.kubeConfig = builder.kubeConfig;
        this.connectionTimeout = builder.connectionTimeout;
        this.readTimeout = builder.readTimeout;
        this.writeTimeout = builder.writeTimeout;
    }


    public static class Builder {
        private String kubeConfigPath;
        private String url;
        private String token;
        private boolean validateSSL;
        private String username;
        private String password;

        private String kubeConfig;
        private int connectionTimeout ;
        private int readTimeout;
        private int writeTimeout;

        public Builder() {
        }
        public <T> Builder kubeConfigPath(String config) {
            this.kubeConfigPath = config;
            return this;
        }
        public <T> Builder url(String url) {
            this.url = url;
            return this;
        }

        public <T> Builder token(String token) {
            this.token = token;
            return this;
        }

        public <T> Builder validateSSL(Boolean validateSSL) {
            this.validateSSL = validateSSL;
            return this;
        }
        public <T> Builder username(String username) {
            this.username = username;
            return this;
        }

        public <T> Builder password(String password) {
            this.password = password;
            return this;
        }
        public <T> Builder kubeConfig(String kubeConfig) {
            this.kubeConfig = kubeConfig;
            return this;
        }

        public <T> Builder connectionTimeout(int connectionTimeout) {
            this.connectionTimeout = connectionTimeout;
            return this;
        }

        public <T> Builder readTimeout(int readTimeout) {
            this.readTimeout = readTimeout;
            return this;
        }
        public <T> Builder writeTimeout(int writeTimeout) {
            this.writeTimeout = writeTimeout;
            return this;
        }

        public KubernetesConfigBuilder build() {
            return new KubernetesConfigBuilder(this);
        }
    }


    public void setKubeConfigPath(String kubeConfigPath) {
        this.kubeConfigPath = kubeConfigPath;
    }

    public String url() {
        return url;
    }


    public String token() {
        return token;
    }


    public boolean validateSSL() {
        return validateSSL;
    }


    public String username() {
        return username;
    }


    public String password() {
        return password;
    }



    public void setUrl(String url) {
        this.url = url;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public void setValidateSSL(boolean validateSSL) {
        this.validateSSL = validateSSL;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setKubeConfig(String kubeConfig) {
        this.kubeConfig = kubeConfig;
    }

    public String getKubeConfigPath() {
        return kubeConfigPath;
    }

    public String getUrl() {
        return url;
    }

    public String getToken() {
        return token;
    }

    public boolean isValidateSSL() {
        return validateSSL;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getKubeConfig() {
        return kubeConfig;
    }

    public int getConnectionTimeout() {
        return connectionTimeout;
    }

    public void setConnectionTimeout(int connectionTimeout) {
        this.connectionTimeout = connectionTimeout;
    }

    public int getReadTimeout() {
        return readTimeout;
    }

    public void setReadTimeout(int readTimeout) {
        this.readTimeout = readTimeout;
    }

    public int getWriteTimeout() {
        return writeTimeout;
    }

    public void setWriteTimeout(int writeTimeout) {
        this.writeTimeout = writeTimeout;
    }
}